<?php
/*
 * Created on Feb 21, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
include('../includes/session.php');
include('../models/realestate_model.php');

class RealestateController{
	
	function RealestateController(){
		global $session;
		if(isset($_POST['catadd'])){
        	$this->catAdd();
     	}else if(isset($_POST['catedit'])){
        	$this->catEdit();
     	}else if(isset($_POST['typeadd'])){
        	$this->typeAdd();
     	}else if(isset($_POST['typeedit'])){
     		$this->typeEdit();
     	}else if(isset($_GET['deleteCategory'])){
     		$this->catDelete();
     	}else{       		
        	header("Location: ".$session->referrer);
       	}
	}
	
	function catAdd(){
		global $session, $form;
		
		$resultValidation = $this->checkValidationCate(
										null,
										$_POST['categoryName'], 
										$_POST['categorySortNo'],
										$_POST['categoryCreateDate']);
	    	
		/* Add Successful */
		if($resultValidation == 0){
	    	$_SESSION['successname'] = $_POST['categoryName'];
	        $_SESSION['success'] = true;
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else if($resultValidation == 1){
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
	    }
	    /* Add attempt failed */
	    else if($resultValidation == 2){
	        $_SESSION['successname'] = $_POST['categoryName'];
	        $_SESSION['success'] = false;
	        header("Location: ".$session->referrer);
		}
	}
	
	function catDelete(){
		global $session, $form, $realestateModel;
		
		$resultValidation = $realestateModel->deleteCategory($_GET['deleteCategory']);
	   
	    if($resultValidation == 0){
	    	$_SESSION['deletecate'] = true;
			$_SESSION['deletename'] = $_GET['deleteCategory'];
	    }else{
	    	$_SESSION['deletecate'] = false;
	    }
		header("Location: ".$session->referrer);
	}
	
	function catEdit(){
		global $session, $form;
		$resultValidation = $this->checkValidationCate(
										$_POST['SelectedCategory'],
										$_POST['categoryName'], 
										$_POST['categoryCreateDate'],
										null);
		
		/* Account edit successful */
	    if($resultValidation == 0){
	    	$_SESSION['edit'] = true;
			$_SESSION['editname'] = $_POST['categoryName'];
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
		}
	}
	
	
	function typeAdd(){
		global $session, $form;
		
		$resultValidation = $this->checkValidationType(
										null,
										null,
										$_POST['name'], 
										$_POST['sortNo'],
										$_POST['type'],
										$_POST['createDate']);
	    	
		/* Add Successful */
		if($resultValidation == 0){
	    	$_SESSION['successnametype'] = $_POST['name'];
	        $_SESSION['successtype'] = true;
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else if($resultValidation == 1){
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
	    }
	    /* Add attempt failed */
	    else if($resultValidation == 2){
	        $_SESSION['successnametype'] = $_POST['name'];
	        $_SESSION['successtype'] = false;
	        header("Location: ".$session->referrer);
		}
	}
	
	function typeEdit(){
		global $session, $form;	
		
		$resultValidation = $this->checkValidationType(
										$_POST['SelectedCategory'],
										$_POST['SelectedType'],
										$_POST['name'],
										$_POST['sortNo'],
										$_POST['type'],										
										null);
		
		/* Account edit successful */
	    if($resultValidation == 0){
	    	$_SESSION['edittype'] = true;
			$_SESSION['editnametype'] = $_POST['name'];
	        header("Location: ".$session->referrer);
	    }
	    /* Error found with form */
	    else{
	        $_SESSION['value_array'] = $_POST;
	        $_SESSION['error_array'] = $form->getErrorArray();
	        header("Location: ".$session->referrer);
		}
	}
	
	function checkValidationCate($id = null, $name, $sortno, $createDate){
		global $form, $realestateModel;
		/* name error checking */
      	$field = "namecategory";  
	    if(!$name || strlen($name = trim($name)) == 0){
	    	$form->setError($field, prnMsg(_('Nhập tên loại hình'),'error'));
	    }
				
		if(!$sortno || strlen($sortno = trim($sortno)) == 0){
	    	$sortno = 0;
	    }
		
		/* Errors exist, have user correct them */
	    if($form->num_errors > 0){
	    	return 1;  //Errors with form
	    }/* No errors, add the new account to the */
	    else{
	    	if(!isset($id)){
		        return $realestateModel->addNewCate($name, $sortno, $createDate);					
			}else{
				return $realestateModel->editCate($id, $name, $sortno);
			}
	   	}
	}
	
	function checkValidationType($cateId = null, $id = null, $name, $sortno, $changeCateId = null, $createDate){
		global $form, $realestateModel;
		/* name error checking */
      	$field = "name";  //Use field name for name
	    if(!$name || strlen($name = trim($name)) == 0){
	    	$form->setError($field, prnMsg(_('Nhập tên loại bất động sản'),'error'));
	    }
		
		$field = "category";  //Use field name for username
	    if($changeCateId <= 0){
	    	$form->setError($field, prnMsg(_('Chọn Loại hình BĐS'),'error'));
	    }
		
		if(!$sortno || strlen($sortno = trim($sortno)) == 0){
	    	$sortno = 0;
	    }
		
		/* Errors exist, have user correct them */
	    if($form->num_errors > 0){
	    	return 1;  //Errors with form
	    }/* No errors, add the new account to the */
	    else{
	    	if(!isset($id)){
		        return $realestateModel->addNewType($name, $sortno, $changeCateId, $createDate);					
			}else{				
				return $realestateModel->editType($cateId, $id, $name, $changeCateId, $sortno);
			}
	   	}
	}
};

/* Create realestate controller object */
$realestateController = new RealestateController;
?>
